package com.zp.configuration;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 闂傚啫娼￠崳閿嬬閹插兒s 闂佹澘绉堕悿?
 */
@Configuration
@ConfigurationProperties(prefix = "oss.client")
@Data
public class AliyunOssClient {

    /**
     * 闁糕晝鍠庨幃?
     */
    private String host;

    /**
     * AccessKeyId
     */
    private String secretId;

    /**
     * AccessKeySecret
     */
    private String secretKey;

    /**
     * OSS Endpoint闁挎稑鑻々? oss-cn-hangzhou.aliyuncs.com
     */
    private String endpoint;

    /**
     * 闁稿繒鍘ч鎰板籍瑜旈崢銈囩磾椤曞棛绐楅柤姹囧劜濠€顓㈠箵閹邦亞杩?endpoint闁挎稑鑻ぐ鏌ユ焻濮樺磭绠?region 缂備礁瀚ˉ?
     */
    private String region;

    /**
     * 閻庢稒锚閸嬪秴顩肩捄鐑樺€崇紒??
     */
    private String bucket;

    /**
     * 闁告帗绋戠紓? OSS 閻庡箍鍨洪崺娑氱博?
     */
    @Bean
    public OSS ossClient() {
        String finalEndpoint = endpoint;
        if (finalEndpoint == null || finalEndpoint.isEmpty()) {
            // 闁稿繒鍘ч鎰版晬濮橆偆鐭?region 闁圭柉澹堥ˉ濠囨晬瀹€?閵囨碍绗熺€ｅ墎绐梒n-hangzhou -> oss-cn-hangzhou.aliyuncs.com
            if (region != null && !region.isEmpty()) {
                finalEndpoint = String.format("oss-%s.aliyuncs.com", region);
            } else {
                throw new IllegalStateException("OSS endpoint 闁哄牜浜崢銈囩磾椤曞棛绀夐悹鍥у槻濠€顏堟煀瀹ュ洨鏋傞柡鍌氭矗濞嗐垺绋夐鈽嗗晭缂??oss.client.endpoint 闁??oss.client.region" );
            }
        }
        return new OSSClientBuilder().build(finalEndpoint, secretId, secretKey);
    }
}




